require(ltm)
require(plink)
require(haven)
require(readr)
require(dplyr)
require(psych)

voter2003 <- read_sav(
  "utas060104.sav", 
  encoding = "CP932", 
  user_na = FALSE
)

variables2003 <- c("q021501", "q021502", "q021503", "q021504", "q021505",
                   "q021506", "q021507", "q021508", "q021509", "q021510", 
                   "q021511", "q021512", "q021513")

for(i in variables2003) {
  n <- voter2003[,i] == 1.5 | voter2003[,i] == 2.5 | 
    voter2003[,i] == 3.5 | voter2003[,i] == 4.5
  n[is.na(n)] <- FALSE
  voter2003[n, i] <- NA
}
voter2003 <- voter2003[rowSums(voter2003[,variables2003], na.rm = TRUE) != 0,]

irt2003_voter <- ltm::grm(voter2003[,variables2003], IRT.param = FALSE)

score2003 <- ltm::factor.scores(irt2003_voter, voter2003[,variables2003])

common2009 <- c("Q013802", "Q013804", "Q013805", "Q013809", 
                "Q013810", "Q013811", "Q013812", "Q013816")
common2003 <- c("q021501", "q021503", "q021504", "q021506", 
                "q021507", "q021508", "q021509", "q021513")
index <- data.frame(
  group1 = match(common2009, colnames(voter2009[,variables2009])), 
  group2 = match(common2003, colnames(voter2003[,variables2003]))
)

res2009 <- coef(irt2009_voter)
res2009 <- res2009[,c(5,1:4)]
colnames(res2009) <- c("a","b1","b2","b3","b4")

res2003 <- coef(irt2003_voter)
res2003 <- res2003[,c(5,1:4)]
colnames(res2003) <- c("a","b1","b2","b3","b4")

pm <- list(res2009, res2003)

rescat <- list(rep(5, nrow(res2009)), 
               rep(5, nrow(res2003)))

pm2009 <- as.poly.mod(n = nrow(res2009), model = "grm", 
                      items = 1:nrow(res2009))
pm2003 <- as.poly.mod(n = nrow(res2003), model = "grm", 
                      items = 1:nrow(res2003))
res <- as.irt.pars(pm, common = index, cat = rescat, 
                   poly.mod = list(pm2009, pm2003), 
                   location = FALSE)

ideology <- list(score2009$score.dat$z1, score2003$score.dat$z1)

plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)

score2003_plink <- link.ability(plink_out)$group2
voter2003$Ideology <- score2003_plink

candidate2005 <- read.csv(
  "2005ates0801171.csv", 
  fileEncoding = "CP932"
)

variables2005_2 <- c("defence", "treaty", "attack", "un", "iraq", 
                     "smallgov", "lifetime", "publecen", "keynes", "consump2", 
                     "postal", "highwayf", "foreign")

candidate2005$iraq <- NA
candidate2005$consump2 <- NA
candidate2005$postal <- NA
candidate2005$highwayf <- NA

for(i in variables2005_2) {
  n <- candidate2005[,i] == 1.5 | candidate2005[,i] == 2.5 | 
    candidate2005[,i] == 3.5 | candidate2005[,i] == 4.5
  n[is.na(n)] <- FALSE
  candidate2005[n, i] <- NA
}
candidate2005 <- candidate2005[rowSums(candidate2005[,variables2005_2], na.rm = TRUE) != 0,]

score2005_2 <- ltm::factor.scores(irt2003_voter, candidate2005[,variables2005_2])
ideology <- list(score2009$score.dat$z1, score2005_2$score.dat$z1)
plink_out2 <- plink(res, common = index, base.grp = 1, 
                    rescale = "MS", ability = ideology)
score2005_2_plink <- link.ability(plink_out2)$group2
candidate2005$Ideology <- score2005_2_plink

elected2005 <- filter(candidate2005, won != 4)
